package com.letv.whatslive.mongo.dao;

import com.letv.whatslive.model.FileUploadLog;
import com.letv.whatslive.model.OpMessage;
import com.letv.whatslive.model.convert.FileUploadLogConvert;
import com.letv.whatslive.model.convert.FirmwareConvert;
import com.letv.whatslive.model.convert.OpMessageConvert;
import com.letv.whatslive.mongo.BaseDAO;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

/**
 * Created by wangjian7 on 2016/10/25.
 */
@Repository
public class FileUploadLogDAO extends BaseDAO{
    private final static Logger logger = LoggerFactory.getLogger(FileUploadLogDAO.class);

    protected String collectionName = "fileUploadLog";


    @Override
    protected void init() {
        super.init(this.collectionName);
    }

    @Override
    protected long getAutoIncrementId() {
        return idGenerate.generateId(this.collectionName);
    }

    public long insertFileUploadLog(FileUploadLog fileUploadLog){
        DBObject obj = FileUploadLogConvert.castFileUploadLogToDBObject(fileUploadLog);
        return this.insert(obj);
    }

    public FileUploadLog getFileUploadLogByMd5(String md5) {
        BasicDBObject query = new BasicDBObject();
        FileUploadLog fileUploadLog = null;
        query.put("md5", md5);
        DBObject dbObject = this.dbCollection.findOne(query);
        if (dbObject == null) return null;
        try {
            fileUploadLog = FileUploadLogConvert.castDBObjectToFileUploadLog(dbObject);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return fileUploadLog;
    }

    public void updateFileUploadLog(FileUploadLog fileUploadLog) {
        DBObject queryCondition = new BasicDBObject();
        queryCondition.put("_id", fileUploadLog.getId());
        BasicDBObject setValue = (BasicDBObject)FileUploadLogConvert.castFileUploadLogToDBObject(fileUploadLog);
        //剔除主键修改
        if (setValue.containsField("_id")) {
            setValue.removeField("_id");
        }
        DBObject values = new BasicDBObject("$set", setValue);
        this.dbCollection.update(queryCondition, values, false, false);
    }

}
